package com.leaves.response;

import com.leaves.exception.MessageCenterException;

import java.util.List;


/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2021年06月21日 10时28分24秒
 * @Description:
 */
public class ApiResult<T> {

    /**
     * 状态码
     */
    public int code;

    /**
     * 描述信息
     */
    public String msg;

    /**
     * 携带对象
     */
    public T data;

    public ApiResult() {
    }

    public ApiResult(int code) {
        this.code = code;
    }

    public ApiResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 将data还原为指定类型并返回
     */
    @SuppressWarnings("unchecked")
    public <T> T getData(Class<T> cs) {
        return (T) data;
    }

    // ============================  构建  ==================================
    // 返回成功
    public static ApiResult success() {
        return new ApiResult(ApiResultConstant.CODE_SUCCESS, "SUCCESS");
    }

    public static ApiResult success(String msg) {
        return new ApiResult(ApiResultConstant.CODE_SUCCESS, msg);
    }

    public static <T> ApiResult<T> success(String msg, T data) {
        return new ApiResult(ApiResultConstant.CODE_SUCCESS, msg, data);
    }

    public static <T> ApiResult<T> successData(T data) {
        return new ApiResult(ApiResultConstant.CODE_SUCCESS, "SUCCESS", data);
    }

    public static <T> ApiResult<T> successArray(T... data) {
        return new ApiResult(ApiResultConstant.CODE_SUCCESS, "SUCCESS", data);
    }

    // 返回失败
    public static ApiResult error() {
        return new ApiResult(ApiResultConstant.CODE_ERROR, "ERROR");
    }

    public static ApiResult error(String msg) {
        return new ApiResult(ApiResultConstant.CODE_ERROR, msg);
    }

    public static ApiResult error(MessageCenterException me) {
        return new ApiResult(ApiResultConstant.CODE_ERROR, me.getMsg());
    }

    // 返回警告
    public static ApiResult warning() {
        return new ApiResult(ApiResultConstant.CODE_ERROR, "WARNING");
    }

    public static ApiResult warning(String msg) {
        return new ApiResult(ApiResultConstant.CODE_WARNING, msg);
    }

    // 返回未登录
    public static ApiResult notLogin() {
        return new ApiResult(ApiResultConstant.CODE_NOT_LOGIN, "Sorry, please login and try again.");
    }

    public static ApiResult checkLogin(String msg) {
        return new ApiResult(ApiResultConstant.CODE_NOT_LOGIN, msg);
    }

    // 返回没有权限的
    public static ApiResult notJur(String msg) {
        return new ApiResult(ApiResultConstant.CODE_NOT_JUR, msg);
    }

    // 返回一个自定义状态码的
    public static ApiResult definition(int code, String msg) {
        return new ApiResult(code, msg, null);
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public String toString() {
        String data_string = null;
        if (data == null) {

        } else if (data instanceof List) {
            data_string = "List（length=" + ((List) data).size() + "）";
        } else {
            data_string = data.toString();
        }
        return "ApiResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
